import Dwarf.DwarfType;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class DwarfInterpreterApplication
{
    private BufferedReader reader;

    public static void main(String args[])
    {
        DwarfInterpreterApplication app = new DwarfInterpreterApplication();
    }

    public DwarfInterpreterApplication()
    {
        reader = new BufferedReader(new InputStreamReader(System.in));
        DwarfType dwarfType = DwarfType.UNDEFINED;
        while (dwarfType == DwarfType.UNDEFINED)
        {
            dwarfType = GetDwarfType();
        }

        DwarfInterpreter dwarfInterpreter = new DwarfInterpreter(dwarfType);
        System.out.println("Input command for dwarf or name of file with commands.\n");
        try
        {
            String str;
            while ((str = reader.readLine()) != null)
            {
                File file = new File(str);
                if (file.isFile())
                {
                    dwarfInterpreter.HandleFile(file);
                }
                else
                {
                    dwarfInterpreter.HandleCodeLine(str);
                }
            }
        }
        catch (IOException ioe)
        {
            System.out.println(ioe.getMessage());
        }
    }

    private DwarfType GetDwarfType()
    {
        DwarfType type = DwarfType.UNDEFINED;
        Map<String, DwarfType> dwarfTypeMap = new HashMap<String, DwarfType>(4);
        dwarfTypeMap.put("1", DwarfType.FARMER);
        dwarfTypeMap.put("2", DwarfType.WARRIOR);
        dwarfTypeMap.put("3", DwarfType.CRAFTSMAN);
        dwarfTypeMap.put("4", DwarfType.MINER);

        try
        {
            System.out.println("Please, input number to select dwarf type:\n1 - FARMER\n2 - WARRIOR\n3 - CRAFTSMAN\n4 - MINER");
            String str = reader.readLine();
            if (dwarfTypeMap.containsKey(str))
            {
                type = dwarfTypeMap.get(str);
            }
            else
            {
                System.out.println("Incorrect input string. Please, repeat input.");
            }
        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return type;
    }

}
